﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web;

namespace Cashflow.DAL
{
	public abstract class ReExProvider : DataAccess
	{
		static private ReExProvider _instance = null;
		static public ReExProvider Instance
		{
			get
			{
				if (_instance == null)
					_instance = (ReExProvider)Activator.CreateInstance(
					   Type.GetType("Cashflow.DAL.SqlClient.SqlReExProvider"));
				return _instance;
			}
		}

		public ReExProvider()
		{
			this.ConnectionString = Globals.Settings.ConnectionString;
			this.EnableCaching = Globals.Settings.EnableCaching;
			this.CacheDuration = Globals.Settings.CacheDuration;
		}

		public abstract ReExDetails GetReExByID(long id);
		public abstract List<ReExDetails> GetReExsFromTo(DateTime from, DateTime to);
		public abstract List<ReExDetails> GetReExsByYear(int year);
		public abstract List<ReExDetails> GetReExsByMonth(int year, int month);
		public abstract List<ReExDetails> GetReExsByDay(int year, int month, int day);
		public abstract int InsertReEx(ReExDetails reex);
		public abstract bool UpdateReEx(ReExDetails reex);
		public abstract bool DeleteReEx(long id);

		protected virtual ReExDetails GetReExFromReader(IDataReader reader)
		{
			return new ReExDetails(
				(long)reader["ID"],
				(DateTime)reader["Date"],
				(decimal)reader["Amount"],
				reader["Note"].ToString());
		}
		protected virtual List<ReExDetails> GetReExCollectionFromReader(IDataReader reader)
		{
			List<ReExDetails> items = new List<ReExDetails>();
			while (reader.Read())
				items.Add(GetReExFromReader(reader));
			return items;
		}
	}
}